package com.todo1.unit_testing.services.sale;

import com.todo1.unit_testing.dto.OrderDto;
import com.todo1.unit_testing.exception.NoStockException;
import com.todo1.unit_testing.exception.SaleException;
import com.todo1.unit_testing.services.customer.CustomerServices;
import com.todo1.unit_testing.services.customer.CustomerServicesImpl;
import com.todo1.unit_testing.services.stock.StockServices;
import com.todo1.unit_testing.services.stock.StockServicesImpl;

public class SalesServicesImpl implements SaleServices {

	private CustomerServices customerServices = new CustomerServicesImpl();
	private StockServices stockServices = new StockServicesImpl();

	public void placeOrder(OrderDto order) throws SaleException {

		boolean availableCredit;
		availableCredit = customerServices.creditAvailable(
				order.getCustomerId(), order.getTotal());

		if (!availableCredit) {
			throw new SaleException("No available credit");
		}
		
		try {
			stockServices.updateStock(order.getDetail());
		} catch (NoStockException e) {
			throw new SaleException(e);
		}
		save(order);
	}

	public void cancelOrder(int orderId) {

	}

	private void save(OrderDto order) {
		System.out.println("Guardando Pedido...");
	}

	public CustomerServices getCustomerServices() {
		return customerServices;
	}

	public void setCustomerServices(CustomerServices customerServices) {
		this.customerServices = customerServices;
	}
}
